package com.fyy.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fyy.base.exception.XCException;
import com.fyy.content.mapper.TeachPlanMapper;
import com.fyy.content.model.dto.AddOrUpdateTeachPlanDTO;
import com.fyy.content.model.dto.TeachPlanDTO;
import com.fyy.content.model.po.TeachPlanPO;
import com.fyy.content.service.TeachPlanService;
import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fyy
 * @version 1.0
 * @description
 * @date 2024/7/21 11:46
 */
@Slf4j
@Service
public class TeachPlanServiceImpl implements TeachPlanService {

    @Autowired
    private TeachPlanMapper teachPlanMapper;

    public List<TeachPlanDTO> selectTreeNodes(Long courseId) {
        return teachPlanMapper.selectTreeNodes(courseId);
    }

    @Override
    public void addOrUpdate(AddOrUpdateTeachPlanDTO addOrUpdateTeachPlanDTO) {
        Long id = addOrUpdateTeachPlanDTO.getId();
        if (id != null) {
            TeachPlanPO teachPlanPO = teachPlanMapper.selectById(id);
            BeanUtils.copyProperties(addOrUpdateTeachPlanDTO, teachPlanPO);
            teachPlanMapper.updateById(teachPlanPO);
        } else {
            int cnt = getTeachPlanCount(addOrUpdateTeachPlanDTO.getCourseId(), addOrUpdateTeachPlanDTO.getParentid());
            TeachPlanPO teachPlanPO = new TeachPlanPO();
            BeanUtils.copyProperties(addOrUpdateTeachPlanDTO, teachPlanPO);
            teachPlanPO.setOrderby(cnt + 1);
            teachPlanMapper.insert(teachPlanPO);
        }
    }

    @Override
    public String deleteById(Long teachPlanId) {
        TeachPlanPO teachPlanPO = teachPlanMapper.selectById(teachPlanId);
        if (teachPlanPO.getGrade() == 2) {
            teachPlanMapper.deleteById(teachPlanId);
            // todo 删除media表中关联的信息
            return "200";
        } else {
            LambdaQueryWrapper<TeachPlanPO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TeachPlanPO::getCourseId, teachPlanPO.getCourseId());
            queryWrapper.eq(TeachPlanPO::getParentid, teachPlanPO.getId());
            Integer cnt = teachPlanMapper.selectCount(queryWrapper);
            if (cnt > 0) {
                XCException.cast("课程计划还有子级信息，无法操作");
            } else {
                teachPlanMapper.deleteById(teachPlanId);
            }
        }
        return "200";
    }

    /**
     * 获取最新的课程计划数
     * @param courseId 课程id
     * @param parentId 父课程计划id
     * @return 课程计划数
     */
    private int getTeachPlanCount(Long courseId, Long parentId) {
        LambdaQueryWrapper<TeachPlanPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachPlanPO::getCourseId, courseId);
        queryWrapper.eq(TeachPlanPO::getParentid, parentId);
        Integer count = teachPlanMapper.selectCount(queryWrapper);
        return count;
    }
}
